//
//  OosMultimediaEntryParser.m
//  oos4ios
//
//  Created by Pablo Gonzalez on 4/29/11.
//  Copyright 2011 11870.com. All rights reserved.
//

#import "OosMultimediaEntryParser.h"
#import <oos4ios/Exceptions/MethodNotImplementedException.h>
#import <oos4ios/Model/OosMultimedia.h>
#import "../Atom/Entries/OosMultimediaEntry.h"
#import <oos4ios/Model/OosMultimedia.h>
#import <oos4ios/Model/OosImage.h>
#import <oos4ios/Model/OosVideo.h>


@implementation OosMultimediaEntryParser

static OosMultimediaEntryParser *instance;

+ (OosMultimediaEntryParser *)instance
{
    if (!instance) {
        instance = [OosMultimediaEntryParser new];
    }
    return instance;
}

- (id)parseEntry:(OosEntry *)entry
{
    if ([entry isKindOfClass: [OosMultimediaEntry class]]) {
        OosMultimediaEntry *multimediaEntry = (OosMultimediaEntry *) entry;
        OosMultimedia *multimedia = nil;
        OosImage *image;
        OosVideo *video;
        NSString *type;
        NSString *identifier;
        NSDate *updated = [entry.updatedDate date];
		type = multimediaEntry.content.type;
		identifier = [multimediaEntry.fnv stringValue];
		if ([type hasPrefix: @"image"]) {
			image = [OosImage imageFromIdentifier: identifier];
			multimedia = image;
		} else if ([type rangeOfString: @"video"].location != NSNotFound) {
			video = [OosVideo videoFromIdentifier: identifier];
			video.snapshotUrlString = multimediaEntry.splash;
			multimedia = video;
		}
		if (multimedia && [multimedia isPreviousTo:updated]) {
			multimedia.updated = updated;
			multimedia.title = [multimediaEntry.title stringValue];
			multimedia.urlString = multimediaEntry.content.sourceURI;
			GDataLink *link = [multimediaEntry linkWithRelAttributeValue:@"alternate"];
			multimedia.webUrlString = link.href;
            NSString *serviceSlug = [multimediaEntry.slug stringValue];
            NSString *userSlug = multimediaEntry.author.slug;
            multimedia.service = [OosService serviceFromSlug: serviceSlug];
            OosReview *review = [OosReview reviewFromServiceSlug:serviceSlug 
                                                     andUserSlug:userSlug];
            multimedia.review = review;
            
		}
        return multimedia;
    } else {
        [NSException raise:@"11870" format:@"Returned entry is not a Multimedia Entry"];
        return nil;
    }
}

- (OosEntry *)reverseParseEntry:(OosObject *)object
{
    return (OosEntry *)object;
}

- (void)reverseParseObject:(OosObject *)object intoEntry:(OosEntry *)entry
{
    [MethodNotImplementedException raiseForSelector:_cmd inObject:self];
}

@end
